package com.tancw.viewacty;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.graphics.pdf.PdfDocument;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import com.tancw.utils.ImgUtil;

import org.json.JSONArray;
import org.json.JSONException;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Objects;


public class DetailActivity extends AppCompatActivity {

    private TextView etUser;
    private TextView etThing;

    private GridView gridView;

    private Button btnBack;

    private Button delDownload;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_detail);
        etUser = findViewById(R.id.dtl_user);
        etThing = findViewById(R.id.dtl_thing);
        btnBack = findViewById(R.id.dtl_back);
        gridView = findViewById(R.id.dtl_grid_img);
        delDownload =findViewById(R.id.dtl_del);

        btnBack.setOnClickListener((v) -> {
            goBack();
        });
        Intent intent = getIntent();
        delDownload.setOnClickListener((v)->{
            doDeleteFile(intent.getStringExtra("fileName"));
        });

        etUser.setText(intent.getStringExtra("user"));
        etThing.setText(intent.getStringExtra("thing"));
        String paths = intent.getStringExtra("imgPaths");
        ImgUtil util = new ImgUtil();
        JSONArray imgs = util.getImgs(paths);

        ArrayList<Bitmap> bitmaps = new ArrayList();
        for (int i = 0; i < imgs.length(); i++) {
            try {
                String path = (String) imgs.get(i);
                bitmaps.add(BitmapFactory.decodeFile(path));
            } catch (JSONException e) {
               e.printStackTrace();
            }
        }

        ImgAdapter imgAdapter = new ImgAdapter(bitmaps);
        gridView.setAdapter(imgAdapter);
    }

    private void doDeleteFile(String fileName){
        new AlertDialog.Builder(DetailActivity.this).setTitle("确认删除" + fileName + "?")
                .setPositiveButton("确认", (dialog, which) -> {
                    if (deleteFile(fileName)) {
                         goBack();
                    }
                }).setNegativeButton("取消", (dialog, which) -> {
                }).show();
    }

    class ImgAdapter extends BaseAdapter {

        private ArrayList<Bitmap> uris;

        public ImgAdapter(ArrayList<Bitmap> uris) {
            this.uris = uris;
        }

        @Override
        public int getCount() {
            return uris.size();
        }

        @Override
        public Object getItem(int position) {
            return uris.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = View.inflate(DetailActivity.this, R.layout.griditem_addpic, null);
            ImageView imageView = view.findViewById(R.id.reg_img);
            imageView.setImageBitmap(uris.get(position));
            imageView.setOnClickListener(v -> {
                Bitmap bitmap = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
                bitmapLoader(bitmap);
            });
            return view;
        }
    }
    private void bitmapLoader(Bitmap bitmap) {
        final Dialog dialog = new Dialog(DetailActivity.this);
        ImageView image = new ImageView(DetailActivity.this);
        image.setImageBitmap(bitmap);
        dialog.setContentView(image);
        Window window = dialog.getWindow();
        window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        dialog.show();
        image.setOnClickListener(v -> {
            dialog.cancel();
        });
    }

    private void goBack() {
        Intent intent = new Intent(DetailActivity.this, MainActivity.class);
        startActivity(intent);
    }

}
